package com.cyy.demo.test;

public class MyThread implements Runnable {

    private  Integer count = 20;

    @Override
    public void run() {

//        synchronized(this){
        System.out.println(Thread.currentThread().getName() + "开始买票");
        synchronized (count) {
            while (count > 0) {
                count--;
                System.out.println(Thread.currentThread().getName() + "买了一张，还剩" + count);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("没票了");

//        }
    }


    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread);
        Thread t2 = new Thread(myThread);
        Thread t3 = new Thread(myThread);
        long start= System.currentTimeMillis();
        t1.start();
        t2.start();
        t3.start();


    }

}

